#include <stdio.h>
#include <stdlib.h>
#include "binio.h"

#define BR false
#define MSB true

#define FORMAT_X "0x%8lx"
#define FORMAT  "%12ld"
#define FICHIER_TRACE "ma_trace"

FILE *bit_stream_in;
FILE *bit_stream_out;

binio_t stream_tab [70];
binio_t in, out;

binio_long_t value;
unsigned int size;
binio_error_t errcode;

void ouvrir (bool msb_first)
{
  in =  binio_read_open ((binio_fgetc_t *) fgetc, 
                         (binio_eof_t *)feof, 
                         (binio_file_func_param_t) bit_stream_in,  
                         msb_first);
  if (in == NULL) 
    {
    fprintf (stderr, "Erreur binio_read_open\n");
    exit (EXIT_FAILURE);
    }
 
  out =  binio_write_open ((binio_fputc_t *)fputc, 
                           (binio_eof_t *) feof,
                           (binio_file_func_param_t) bit_stream_out,
                           msb_first);
  if (out == NULL) 
    {
    fprintf (stderr, "Erreur binio_write_open\n");
    exit (EXIT_FAILURE);
    }
} 

void afficher (char *message)
{
  fprintf (stderr,  "%s ",message);
  fprintf (stderr,  FORMAT , (long) value);
  fprintf (stderr, " ( " FORMAT_X  " ) ", (long) value);
  fprintf (stderr, " sur %d bits :",size);
  fprintf (stderr, " errcode = %d",errcode);
  fprintf (stderr,"\n");
}

void ecrire (bool br)
{

  value = -783;
  size = 12;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = 5;
  size = 4;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -6;
  size = 6;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -6;
  size = 4;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = 0;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = 1002;
  size = 11;

  value = 1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -987;
  size = 11;


  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  
  value = 0xabcdef;
  size = 25;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = 6;
  size = 4;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = 0;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  value = -1;
  size = 1;

  errcode = binio_write (out,  value, size  , br);
  afficher ("Ecriture ");

  binio_close (out);
  fclose (bit_stream_out);
}

void lire (bool br)
{
  size = 12;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 4;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 6;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 4;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 11;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 25;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 1;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");

  size = 4;
  errcode = binio_read (in, &value, size, br);
  afficher ("Lecture  ");


  fclose (bit_stream_in);
}

int main (int argc, char *argv[], char *envp[])
{
  binio_error_t code;

  // Ecriture + lecture unsigned

  bool br = BR;
  bool msb = MSB;

  code = binio_init (2);
  fprintf (stderr,"code retour init= %d\n",code);

  bit_stream_out = fopen (FICHIER_TRACE,"w");
  bit_stream_in = fopen (FICHIER_TRACE,"r");

  ouvrir (msb);
  fprintf (stderr, "Ouverture ok\n");

  fprintf (stderr, "\n");

  ecrire (br);
  fprintf (stderr,"\n");
  lire (br);
  fprintf (stderr,"\n");

  return 0;
}
